package com.hama.iotrust.common.atmosphere;

import java.util.List;

import org.atmosphere.config.service.WebSocketProtocolService;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.websocket.WebSocket;
import org.atmosphere.websocket.WebSocketProcessor;
import org.atmosphere.websocket.WebSocketProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hama.iotrust.common.atmosphere.DelegatingWebSocketProtocol;
 
@WebSocketProtocolService
public class DelegatingWebSocketProtocol implements WebSocketProtocol {

    public static final Logger logger = LoggerFactory.getLogger(DelegatingWebSocketProtocol.class);

    public void configure(AtmosphereConfig atmosphereConfig) {

    }

    public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) {
        if (webSocket.resource() == null) {
            return null;
        }

        logger.debug(message);
        return null;
    }

    public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] bytes, int offset, int length) {
        return onMessage(webSocket, new String(bytes, offset, length));
    }

    public void onOpen(WebSocket webSocket) {
        logger.debug("Opened WebSocket connection {}", webSocket);
    }

    public void onClose(WebSocket webSocket) {
        logger.debug("Closing WebSocket connection {}", webSocket);
    }

    public void onError(WebSocket webSocket, WebSocketProcessor.WebSocketException e) {
        logger.error("Error on WebSocket connection {}", e);
    }
}
